package de.jmda.gen.java.impl;

import static de.jmda.core.util.StringUtil.sb;
import de.jmda.core.util.StringUtil;
import de.jmda.gen.GeneratorException;
import de.jmda.gen.LineIndenter;
import de.jmda.gen.impl.DefaultGenerator;
import de.jmda.gen.java.MethodBodyGenerator;

public class DefaultMethodBodyGenerator
		extends DefaultGenerator
		implements MethodBodyGenerator
{
	private StringBuffer methodBodyContent;
	
	public DefaultMethodBodyGenerator()
	{
		this("", new LineIndenter("\t", 0));
	}

	public DefaultMethodBodyGenerator(String methodBodyContent)
	{
		this(methodBodyContent, new LineIndenter("\t", 0));
	}

	public DefaultMethodBodyGenerator(
			String methodBodyContent, LineIndenter lineIndenter)
	{
		super(methodBodyContent, lineIndenter);

		this.methodBodyContent = sb(methodBodyContent);
	}

	/**
	 * @return {@link #methodBodyContent} included in
	 * <pre>
	 * {
	 * }
	 * </pre>
	 *
	 * @see de.jmda.gen.impl.DefaultGenerator#generate()
	 */
	@Override
	public StringBuffer generate() throws GeneratorException
	{
		LineIndenter contentLineIndenter =
				new LineIndenter(
						getLineIndenter().getIndentation(),
						getLineIndenter().getIndentationLevel() + 1);
		
		StringBuffer content = contentLineIndenter.indent(methodBodyContent);
		
		if (false == StringUtil.isBlank(content))
		{
			content.append(System.lineSeparator());
		}

		return sb("{" + System.lineSeparator() + content + "}");
	}

	@Override
  public StringBuffer getMethodBody() throws GeneratorException
  {
	  return generate();
  }

	@Override
  public void setMethodBody(String methodBodyContent)
  {
		this.methodBodyContent = sb(methodBodyContent);
  }
}